﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;

using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiTienTe;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_BoPhan;
using csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan;
using csla4Skeleton.UI.SL.DanhMuc.DM_DonViTinh;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_KeKhai;
using csla4Skeleton.UI.SL.DanhMuc.DM_TinhChatHangHoa;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;

namespace csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_GhiGiam
{
    public partial class TSCD_GhiGiam_MainWindow: UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public string Title
        {
            get { return "GhiGiam"; }
        }

       
        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
      
        public TSCD_GhiGiam_MainWindow(int Khoa)          
        {
            this.Khoa = Khoa;
        }

        public TSCD_GhiGiam_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_GhiGiam.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            
            InitializeComponent();  
            ControlBehavior.ControlBehaviors.Navigation(this.TSCD_GhiGiam_ChungTuInformation_GroupBox, controls);
            ControlBehavior.ControlBehaviors.Navigation(this.TSCD_GhiGiam_MoreInformation_GroupBox, controls);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                
                System.Windows.Data.CollectionViewSource TSCD_GhiGiam_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_GhiGiam_ViewModelSource"];
                if (Khoa == null)
                {
                    TSCD_GhiGiam_ViewModel TSCD_GhiGiam_ViewModel = new TSCD_GhiGiam_ViewModel();
                    TSCD_GhiGiam_ViewModel.ChildList_AddedNew += new TSCD_GhiGiam_ViewModel.ChildList_AddedNew_EventHandler(TSCD_GhiGiam_ViewModel_ChildList_AddedNew);
                    
                    TSCD_GhiGiam_ViewModel.TCHangHoaChanged += new EventHandler(TSCD_GhiGiam_TinhChatHangHoaChanged);
                    TSCD_GhiGiam_ViewModel.tscdIdChanged += new TSCD_GhiGiam_ViewModel.TSCDIdChanged_EventHandler(TSCD_GhiGiam_TSCDIdChanged);
                   
                    TSCD_GhiGiam_ViewModel.LoaiTienTeChanged += new EventHandler(TSCD_GhiGiam_LoaiTienTeChanged);

                    TSCD_GhiGiam_CollectionViewSource.Source = new List<object> { TSCD_GhiGiam_ViewModel };

                    
                }
                else
                {
                    TSCD_GhiGiam_ViewModel TSCD_GhiGiam_ViewModel = new TSCD_GhiGiam_ViewModel((int)Khoa);
                    TSCD_GhiGiam_ViewModel.ChildList_AddedNew += new TSCD_GhiGiam_ViewModel.ChildList_AddedNew_EventHandler(TSCD_GhiGiam_ViewModel_ChildList_AddedNew);
                    TSCD_GhiGiam_ViewModel.TCHangHoaChanged += new EventHandler(TSCD_GhiGiam_TinhChatHangHoaChanged);
                    TSCD_GhiGiam_ViewModel.tscdIdChanged += new TSCD_GhiGiam_ViewModel.TSCDIdChanged_EventHandler(TSCD_GhiGiam_TSCDIdChanged);
                    TSCD_GhiGiam_ViewModel.LoaiTienTeChanged += new EventHandler(TSCD_GhiGiam_LoaiTienTeChanged);
                    TSCD_GhiGiam_CollectionViewSource.Source = new List<object> { TSCD_GhiGiam_ViewModel };

                    
                }

                System.Windows.Data.CollectionViewSource dm_BoPhan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_BoPhan_ListViewModelSource"];
                DM_BoPhan_ListViewModel dm_BoPhan_ListViewModel = new DM_BoPhan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_BoPhan_CollectionViewSource.Source = new List<object> { dm_BoPhan_ListViewModel };

               
                System.Windows.Data.CollectionViewSource dm_LoaiTienTe_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
                DM_LoaiTienTe_ListViewModel dm_LoaiTienTe_ListViewModel = new DM_LoaiTienTe_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiTienTe_CollectionViewSource.Source = new List<object> { dm_LoaiTienTe_ListViewModel };


                System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanNo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_ChiTiet_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanCo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_ChiTiet_ListViewModel };

                /*System.Windows.Data.CollectionViewSource dm_TinhChatHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TinhChatHangHoa_ListViewModelSource"];
                DM_TinhChatHangHoa_ListViewModel dm_TinhChatHangHoa_ListViewModel = new DM_TinhChatHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TinhChatHangHoa_CollectionViewSource.Source = new List<object> { dm_TinhChatHangHoa_ListViewModel };
                */
              
               
                DoFilterOnDefaultDM_SanPham();

                this.TSCD_GhiGiam_GhiGiamChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TSCD_GhiGiam_GhiGiamChiTietGridView);
                
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TSCD_GhiGiam_GhiGiamChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TSCD_GhiGiam_GhiGiamChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
               
            }
        }

        #endregion
       

        #region Event Handle
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                TSCD_GhiGiam_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                else if (e.Key == Key.D1)
                {
                    this.TSCD_GhiGiam_GhiGiamChiTietTab.IsSelected = true;
                }
               
               
                
            }
            e.Handled = true;
        }

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {

            AddItems();
        }

        void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();
          
        }

        public void AddItems()
        {
            if (this.TSCD_GhiGiam_GhiGiamChiTietTab.IsSelected)
            {
                AddGhiGiamDetailsItem();
            }
        }
        private RadGridView GetActiveGridView()
        {
            if (this.TSCD_GhiGiam_GhiGiamChiTietTab.IsSelected)
            {
                return TSCD_GhiGiam_GhiGiamChiTietGridView;
            }
           
            return null;
        }



        void DeleteItems()
        {
           if (this.TSCD_GhiGiam_GhiGiamChiTietTab.IsSelected)
            {
                TSCD_GhiGiam_ChiTietChild ChiTietChild = (TSCD_GhiGiam_ChiTietChild)(this.TSCD_GhiGiam_GhiGiamChiTietGridView.SelectedItem);
                IList<TSCD_GhiGiam_ChiTietChild> list = GetViewModel().Model.TSCD_GhiGiam_ChiTiets;
                int index = list.IndexOf(ChiTietChild);
                

                DeleteGhiGiamDeTailsItem(index);
            }
           
            
        }
           
        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
            }
        }
              
        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
           TSCD_GhiGiam_ViewModel vm = GetViewModel();
            vm.SaveAndNew();
        }

        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddGhiGiamDetailsItem();
                }
            }
            else
            {
                ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
            }
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddGhiGiamDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
            }
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddGhiGiamDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
            }
        }


        private void BookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().GhiSo();
        }

        private void DeleteBookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().BoGhi();
        }

        void OnTabKeyPress(object sender, EventArgs e)
        {
            this.tabControl.SelectedItem = tabControl.Items[0];
            this.AddGhiGiamDetailsItem();
        }

        void OnReachLastColumn(object sender, ReachLastColumnArgs e)
        {

            List<TabItem> AvailableTabItem = new List<TabItem>();
            int indexCurrentTab = 0;
            foreach (TabItem item in this.tabControl.Items)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    AvailableTabItem.Add(item);
                    if (item.GetHashCode() == tabControl.SelectedItem.GetHashCode())
                    {
                        indexCurrentTab = AvailableTabItem.IndexOf(item);
                    }

                }
            }


            TabItem[] list = AvailableTabItem.ToArray();
            if (indexCurrentTab == (AvailableTabItem.Count - 1))
            {
                this.tabControl.SelectedItem = list[0];
                FocusAfterChangeTab(this.TSCD_GhiGiam_GhiGiamChiTietGridView, e.Index);

            }
            

        }

        private void FocusAfterChangeTab(GridViewDataControl grid, int index)
        {
             
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[index], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
           
        }
        
        private void TSCD_GhiGiam_MoreInformation_LoaiTienId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiTienTe_ChildWindow dm_LoaiTienTe_ChildWindow = new DM_LoaiTienTe_ChildWindow();
            dm_LoaiTienTe_ChildWindow.Show();
            dm_LoaiTienTe_ChildWindow.SaveClicked += new EventHandler(dm_LoaiTienTe_ViewModel_Saved);
        }

       
        #endregion

        #region View Model Related Methods
        private TSCD_GhiGiam_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource TSCD_GhiGiam_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_GhiGiam_ViewModelSource"];
            return ((List<object>)TSCD_GhiGiam_CollectionViewSource.Source)[0] as TSCD_GhiGiam_ViewModel;
        }

       
        void dm_LoaiTienTe_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiTienTe_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
            DM_LoaiTienTe_ListViewModel vm = ((List<object>)dm_LoaiTienTe_ListViewSource.Source)[0] as DM_LoaiTienTe_ListViewModel;
            vm.GetDM_LoaiTienTe_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        public void DoFilterOnDefaultDM_SanPham()
        {

            System.Windows.Data.CollectionViewSource TSCD_KeKhai_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KeKhai_ListViewModelSource"];
            TSCD_KeKhai_ListViewModel tscd_KeKhai_ListViewModel = new TSCD_KeKhai_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            TSCD_KeKhai_CollectionViewSource.Source = new List<object> { tscd_KeKhai_ListViewModel };
           
        }
     
  

     
        public void TSCD_GhiGiam_TinhChatHangHoaChanged(object sender, EventArgs e)
        {
            /*int? tinhChatHangHoaId = this.GetViewModel().Model.TinhChatHangHoaId;
            
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            dm_HangHoa_ListViewModel.GetDM_HangHoa((int)tinhChatHangHoaId);
             */ 
           
        }

        void TSCD_GhiGiam_LoaiTienTeChanged(object sender, EventArgs e)
        {
            
            System.Windows.Data.CollectionViewSource dm_LoaiTienTe_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
            DM_LoaiTienTe_ListViewModel dm_LoaiTienTe_ListViewModel = ((List<object>)dm_LoaiTienTe_CollectionViewSource.Source)[0] as DM_LoaiTienTe_ListViewModel;
            IList<DM_LoaiTienTeInfo> list = dm_LoaiTienTe_ListViewModel.Model.getListOfInfo();
            foreach( DM_LoaiTienTeInfo item in list)
            {
                if (item.Khoa == GetViewModel().Model.LoaiTienTeId)
                {
                    GetViewModel().Model.TyGia = item.TyGiaQuyDoi;
                    break;
                }
            }
        }
       
        
        void TSCD_GhiGiam_TSCDIdChanged(object sender, TSCDIdChangedEventArgs e)
        {
           
            TSCD_GhiGiam_ChiTietChild ChiTietChild =  GetViewModel().Model.TSCD_GhiGiam_ChiTiets[e.Index];

            System.Windows.Data.CollectionViewSource TSCD_KeKhai_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KeKhai_ListViewModelSource"];
            TSCD_KeKhai_ListViewModel tscd_KeKhai_ListViewModel = ((List<object>)TSCD_KeKhai_CollectionViewSource.Source)[0] as TSCD_KeKhai_ListViewModel;
            System.Collections.Generic.IList<TSCD_KeKhaiInfo> list = tscd_KeKhai_ListViewModel.Model.getListOfInfo();


            foreach (TSCD_KeKhaiInfo item in list)
            {
                if (item.Khoa == e.TSCDId)
                {
                    ChiTietChild.DienGiai = item.Ten;
                    ChiTietChild.BoPhanId = item.BoPhanId;
                    ChiTietChild.NguyenGia= (double) item.NguyenGia;
                    break;
                }
            }
           

     
        }
     

        

        private void AddGhiGiamDetailsItem()
        {
            GetViewModel().AddTSCD_GhiGiam_ChiTietItem();
        }

       
        


        private void DeleteGhiGiamDeTailsItem(int Index)
        {
            GetViewModel().DeleteTSCD_GhiGiam_ChiTietItem( Index);
        }

       
        

        void TSCD_GhiGiam_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "TSCD_GhiGiam_ChiTiets")
            {
                this.TSCD_GhiGiam_GhiGiamChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
           
            
        }

       
        #endregion
        
    }
}
